<?php
/**
 * Created by PhpStorm.
 * User: Love
 * Date: 2018/8/8
 * Time: 18:05
 */

namespace App\Logic;


use App\Area;
use App\Item;
use App\Member;
use Ionepub\Idcard;
use Log;

/**
 * @property \App\Craftsman model
 */
class Craftsman
{

    /**
     * @param array $data 表数据
     * @param array $header 班组长数据
     * @return array|bool
     */
    public function insertCraftsman($data, $header)
    {
        $images = [];
        foreach ($data['images'] as $value) {
            array_push($images, $value['name']);
        }
        $data['images'] = $images;
        $header_id = $this->addMember($header);
        $data['header_id'] = $header_id;
        $res = \App\Craftsman::create($data);
        if ($res->id) {
            $member = Member::findOrFail($header_id);
            $member->craftmans = $res->id;
            $member->save();
            return ['res'=>$res->id,'header'=>$header_id];
        }
        return false;
    }
    public function addMember($data)
    {
        $this->_genCity($data);
        $header_images = [];
        foreach ($data['headman_images'] as $value) {
            array_push($header_images, $value['name']);
        }
        unset($data['headman_images']);
        $data['images'] = $header_images;

        return $this->createHeader($data);
    }

    public function updateMember($data)
    {
        $this->_genCity($data);
        $header_images = [];
        foreach ($data['headman_images'] as $value) {
            array_push($header_images, $value['name']);
        }
        unset($data['headman_images']);
        $data['images'] = $header_images;
        return $this->updateHeader($data);
    }

    public function updateHeader(array $header)
    {
        if (!empty($header['card'])) {
            $idCard = Idcard::getInstance($header['card']);
            $header['sex'] = $idCard->getGender() == '男' ? 1 : 2;
            $header['age'] = $idCard->getAge();
            $header['origin'] = $idCard->getRegion();
        }
        $header['images'] = json_encode($header['images']).'';
        // var_dump($header);die;
        // print_r($header);
        // die;
        $res = Member::whereId($header['id'])->update($header);
        return $header['id'];
    }
    /**
     * @param array $data
     */
    private function _genCity(array &$data)
    {
        $city = $data['city'];
        unset($data['city']);
        $this->_insertCity($city);
        $hope_city = $data['hope_city'];
        $data['hope_city'] = str_replace("直辖市", "", $hope_city[0]['name'] . $hope_city[1]['name']);
        $data = array_merge($data, ['province' => $city[0]['code'], 'city' => $city[1]['code'], 'area' => $city[2]['code']??0, 'road' => $city[3]['code']??0]);
    }

    private function _insertCity($city)
    {
        foreach ($city as $key => $value) {
            $parent = $key == 0 ? 0 : $city[$key - 1]['code'];
            $res = Area::where('id', $value['code'])->get();

            if ($res->isEmpty())
                if(isset($value['code'])){
                Area::insert(['id' => $value['code'], 'name' => $value['name'], 'pid' => $parent]);
                }
        }
    }

    private function createHeader(array $header)
    {
        if (!empty($header['card'])) {
            $idCard = Idcard::getInstance($header['card']);
            $header['sex'] = $idCard->getGender() == '男' ? 1 : 2;
            $header['age'] = $idCard->getAge();
            $header['origin'] = $idCard->getRegion();
        }
        $res = Member::create($header);
        return $res->id;
    }

    public function addItem(array $data)
    {
        $city = $data['city'];
        $this->_insertCity($city);
        $data['province'] = $city[0]['code'];
        $data['city'] = $city[1]['code'];
//        unset($data['city']);
        $item = Item::create($data);
        return $item->id;
    }

    public function update($data, $header){
        $images = [];
        foreach ($data['images'] as $value) {
            array_push($images, $value['name']);
        }
        $data['images'] = json_encode($images);
        $header_id = $this->updateMember($header);
        $data['header_id'] = $header_id;
        $res = \App\Craftsman::whereId($data['id'])->update($data);
        if ($res) {
            return ['res'=>$data['id'],'header'=>$header_id];
        }
        return false;
    }
}